<template>
  <div class="full">
    <div class="flex-container">
      <img src="../assets/房子.png" width="40" height="40" />
      <div class="top-title">村级经济</div>
    </div>
    <div class="container">
      <div class="full-secondtitle">
        <div class="h1">年度收入统计</div>
      </div>
    </div>
    <div class="box">
      <div class="flex-container">
        <div class="flex-cyl-item">
          <div class="flex-cyl-text">
            <!-- 数字 -->
            <div class="cyl-number">{{ rate1 }}0</div>
            <div class="cyl-text1">亿元</div>
            <!-- 文字 -->
          </div>
          <span class="cyl-text2">{{ text1 }}全村总资产</span>
        </div>

        <div class="flex-cyl-item">
          <div class="flex-cyl-text">
            <!-- 数字 -->
            <div class="cyl-number">{{ rate1 }}0</div>
            <div class="cyl-text1">亿元</div>
            <!-- 文字 -->
          </div>
          <span class="cyl-text2">{{ text1 }}工业销售收入</span>
        </div>
        <div class="flex-cyl-item">
          <div class="flex-cyl-text">
            <!-- 数字 -->
            <div class="cyl-number">{{ rate1 }}0</div>
            <div class="cyl-text1">万元</div>
            <!-- 文字 -->
          </div>
          <span class="cyl-text2">{{ text1 }}可用财力</span>
        </div>
      </div>
    </div>
    <div class="container">
      <div class="full-secondtitle">
        <div class="h1">收入组成占比</div>
      </div>
    </div>

    <div class="flex-container">
      <div class="cht30">
        <v-chart
          id="chart30"
          ref="chart30"
          class="chart30"
          :option="option30"
        />
      </div>
    </div>
    <div class="container">
      <div class="full-secondtitle">
        <div class="h1">村内资产分布</div>
      </div>
    </div>
    <div class="cht31">
      <v-chart id="chart31" ref="chart31" class="chart31" :option="option31" />
    </div>
    <div class="container">
      <div class="full-secondtitle">
        <div class="h1">近一年村内支出金额</div>
      </div>
    </div>
    <div class="cht32">
      <v-chart id="chart32" class="chart32" :option="option32" />
    </div>
  </div>
</template>

<script >
// var chartDom = document.getElementById("main");postall
// var myChart = echarts.init(chartDom);
import * as echarts from "echarts";
import { ref, onMounted, watch } from "vue";
import axios from "axios";
import { getarticle } from "../reqeust/index";
export default {
  data() {
    return {
      // option31: {}, // 存储echarts图表的配置项
      option32: {}, // 存储echarts图表的配置项
    };
  },
  setup() {
    const chart31 = ref(null);
    const option31 = ref(null);
    const chart30 = ref(null);
    const option30 = ref(null);
    async function fetchData() {
      try {
        option31.value = {
          // 根据需要配置echarts的option
          backgroundColor: "transparent",
          color: ["#c73232", "#8f2323", "#ba2822", "#e62f29"],
          tooltip: {
            trigger: "item",
            textStyle: {
              fontSize: 16, // 设置图例文字大小
            },
          },
          series: [
            {
              name: "事件类型",
              type: "pie",
              radius: [35, 65],
              // roseType: "radius",
              data: [
                { value: 20, name: 132 },
                { value: 10, name: 122 },
              ],
              label: {
                color: "#ffffff",
                // alignTo: "edge",
                formatter: function (params) {
                  var percent = ((params.value / 30) * 100).toFixed(0);
                  return percent + "%" + "\n" + params.name;
                },
                // minMargin: 50,
                edgeDistance: 10,
                lineHeight: 18,
                fontSize: 16,
                rich: {
                  time: {},
                },
              },
              labelLine: {
                length: 10,
                minTurnAngle: 150,
                length2: 50,
              },
              emphasis: {
                itemStyle: {
                  shadowBlur: 10,
                  shadowOffsetX: 0,
                  shadowColor: "rgba(0, 0, 0, 0.5)",
                },
              },
            },
          ],
        };
        option30.value = {
          tooltip: {
            // trigger: "axis",
            // axisPointer: {
            //   // Use axis to trigger tooltip
            //   type: "shadow", // 'shadow' as default; can also be 'line' or 'shadow'
            // },
          },
          legend: {
            top: "10%",
            show: true,
            textStyle: {
              color: "rgba(255, 255, 255, 1)",
            },
          },
          grid: {
            top: "0%",
            left: "0%",
            right: "0%",
            bottom: "30%",
          },
          xAxis: { show: false, type: "value", max: 100 },
          yAxis: { show: false, type: "category" },
          series: [
            {
              name: "123",
              type: "bar",
              stack: "total",
              barWidth: 40,
              label: {
                show: true,
                fontSize: 16,
                fontWeight: "bold",
                formatter: function (params) {
                  var percent = ((params.value / 100) * 100).toFixed(0);
                  return percent + "%" + "\n" + params.name;
                },
              },

              emphasis: {
                focus: "series",
              },
              data: [{ value: 20, name: 123 }],
            },
            {
              name: "Mail Ad",
              type: "bar",
              stack: "total",
              label: {
                show: true,
                fontSize: 16,
                fontWeight: "bold",
                formatter: function (params) {
                  var percent = ((params.value / 100) * 100).toFixed(0);
                  return percent + "%" + "\n" + params.name;
                },
              },
              emphasis: {
                focus: "series",
              },
              data: [{ value: 20, name: 123 }],
            },
            {
              name: "Affiliate Ad",
              type: "bar",
              stack: "total",
              label: {
                show: true,
                fontSize: 16,
                fontWeight: "bold",
                formatter: function (params) {
                  var percent = ((params.value / 100) * 100).toFixed(0);
                  return percent + "%" + "\n" + params.name;
                },
              },
              emphasis: {
                focus: "series",
              },
              data: [{ value: 20, name: 123 }],
            },
            {
              name: "Video Ad",
              type: "bar",
              stack: "total",
              label: {
                show: true,
                fontSize: 16,
                fontWeight: "bold",
                formatter: function (params) {
                  var percent = ((params.value / 100) * 100).toFixed(0);
                  return percent + "%" + "\n" + params.name;
                },
              },
              emphasis: {
                focus: "series",
              },
              data: [{ value: 20, name: 123 }],
            },
          ],
        };
      } catch (error) {
        console.error("Failed to fetch data:", error);
      }
    }
    // 初始化echarts图表
    function initChart() {
      chart31.value = echarts.init(document.getElementById("chart31"));
      chart30.value = echarts.init(document.getElementById("chart30"));

      watch(
        option31,
        () => {
          chart31.value.setOption(option31.value);
          chart30.value.setOption(option30.value);
        },
        { deep: true }
      );
    }
    onMounted(() => {
      initChart();
      fetchData();
    });
    return {
      option31,
      option30,
    };
  },
  async mounted() {
    this.getDataFromAPI();
  },
  methods: {
    async getDataFromAPI() {
      try {
        this.option32 = {
          backgroundColor: "transparent",
          tooltip: {
            trigger: "axis",
            textStyle: {
              fontSize: 10, // 设置图例文字大小
            },
            axisPointer: {
              type: "cross",
              crossStyle: {
                color: "#999",
              },
            },
          },
          grid: {
            top: "18%",
            left: "10%",
            bottom: "10%",
            tooltip: {
              axisPointer: {
                label: {
                  fontSize: 10,
                },
              },
            },
          },

          xAxis: {
            type: "category",
            data: [
              "Mon",
              "Tue",
              "Wed",
              "Thu",
              "Fri",
              "Sat",
              "Sun",
              "Mon",
              "Tue",
              "Wed",
              "Thu",
              "Fri",
            ],
            axisPointer: {
              type: "shadow",
            },
            nameTextStyle: {
              fontSize: 10,
            },
            axisLabel: {
              interval: 0,
              fontSize: 10,
              color: "#ffffff", // 设置图例文字大小
            },
            axisTick: {
              show: false,
            },
          },

          yAxis: {
            type: "value",
            name: "件",
            min: 0,
            max: 200,
            // interval: 1,
            nameTextStyle: {
              fontSize: 10,
              color: "#ffffff", // 设置图例文字大
              align: "right",
            },
            axisLine: {
              show: true,
              lineStyle: {
                width: 1,
              },
            },
            axisLabel: {
              hideOverlap: true,
              interval: 0,
              fontSize: 10,
              formatter: "{value} ",
              color: "#ffffff", // 设置图例文字大小
            },
            splitLine: {
              show: false, // 不显示网格线
            },
          },
          series: [
            {
              name: "事件总数",
              type: "bar",
              barWidth: 10,
              itemStyle: {
                borderColor: "#3fb1e3",
                color: function (params) {
                  // 设置白色渐变和红色渐变的间隔
                  return new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                    { offset: 0, color: "#3fb1e340" },
                    { offset: 0.1, color: "#3fb1e3" },
                    { offset: 0.5, color: "#3fb1e365" },
                    { offset: 1, color: "#3fb1e300" },
                  ]);
                },
              },
              tooltip: {
                valueFormatter: function (value) {
                  return value + " 件";
                },
              },
              label: {
                show: true,
                position: "top",
                itemWidth: 8,
                itemHeight: 5,
                textStyle: {
                  fontSize: 10,
                  color: "#ffffff", // 设置图例文字大小
                },
              },
              data: [120, 200, 150, 80, 70, 110, 130, 70, 110, 130],
            },
          ],
        };
        this.renderChart(); // 渲染图表
      } catch (error) {
        // 错误处理
        console.error(error);
      }
    },
    updateChart(newData) {
      this.option31.series[0].data = newData;
    },
    renderChart() {
      // const mychart31 = echarts.init(document.getElementById("chart31"), "dark"); // 初始化echarts图表
      // mychart31.setOption(this.option31); // 设置图表的配置项
      const myChart32 = echarts.init(
        document.getElementById("chart32"),
        "dark"
      ); // 初始化echarts图表
      myChart32.setOption(this.option32); // 设置图表的配置项
    },
  },
};
</script>
<style scoped>
.full {
  display: flex;
  flex-direction: column;
  width: 25rem;
  /* background-color: #e62f29; */
}
.top-title {
  height: 2.25rem;
  font-size: 2.25rem;
  font-weight: 600;
  font-family: "MyFontPFS", sans-serif;
  color: #ffffff;
  line-height: 2rem;
  background: linear-gradient(0deg, #5677a4 0%, rgb(255, 255, 255) 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  margin-left: 1rem;
}
.container {
  display: flex;
  justify-content: space-between;
  margin-top: 0.5rem;
  margin-bottom: 0.5rem;
}
.full-secondtitle {
  display: flex;
  align-items: center;
  background-color: #ffffff70;
  padding: 0.3rem 1rem;
}
.full-secondtitle .h1 {
  line-height: 1;
  font-size: 1.4rem;

  font-weight: 600;
  color: #ffffff;
}
.month-selector {
  display: flex;
  flex-direction: column;
  position: absolute;
  top: 0;
  right: 10%;
}
.select1 {
  height: 3rem;
  width: 3.5rem;
  text-align: center;
  font-size: 1.5rem;

  font-weight: 400;
  color: #ffffff;
  line-height: 3rem;
}
.select1 option {
  background-color: transparent;
}
select {
  border-radius: 10px;
  border: 2px solid rgb(0, 255, 255); /* 设置边框宽度和颜色为蓝色 */
  box-shadow: 0 0 20px rgb(0, 208, 255); /* 添加阴影效果 */
  transition: border-color 0.3s, box-shadow 0.3s; /* 添加过渡效果，使改变边框和阴影时平滑过渡 */
  -webkit-appearance: none;
  -moz-appearance: none;
  appearance: none;
  color: #ffffff;
  background-color: transparent;
}
option {
  background-color: transparent;
  color: #000000;
  border: 2px solid rgb(0, 255, 255);
  box-shadow: 0 0 20px rgb(0, 208, 255);
}
select:focus {
  /* color: #000000; */
  outline: none;
}
select::-ms-expand {
  display: none;
}
select::-webkit-outer-spin-button,
select::-webkit-inner-spin-button {
  -webkit-appearance: none;
  margin: 0;
}
.right-top {
  align-self: flex-start; /* 靠右对齐 */
  margin-left: 1rem;
}
.box {
  width: 25rem;
  display: flex;
  flex-direction: column;
  align-items: center;
}
.flex-container {
  display: flex;
  flex-direction: row;
  justify-content: center;
  align-items: center;
  flex-grow: 1;
  width: 25rem;
  border: 0.08rem solid rgba(255, 255, 255, 0.575);
  padding: 0.3rem 1rem;
}
.flex-item {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  flex-wrap: nowrap;
  /* margin: auto; */
  flex-grow: 1;
  flex-basis: 33.33%;
}
.image {
  flex-flow: 1;
}

.number {
  font-size: 2.2rem;

  font-weight: 600;
  color: #f2f1f1;
}
.text {
  font-size: 1.2rem;

  font-weight: 400;
  color: #d9d9d9;
}
.cht31 {
  width: 25rem;
  height: 10rem;
  border: 0.08rem solid rgba(255, 255, 255, 0.575);
}
.cht30 {
  margin-top: -1.2rem;
  width: 24rem;
  height: 7rem;
}
.cht32 {
  width: 25rem;
  height: 13rem;
  border: 0.08rem solid rgba(255, 255, 255, 0.575);
}
.chart31 {
  width: 25rem;
  height: 10rem;
}
.chart30 {
  width: 24rem;
  height: 14rem;
}

.chart32 {
  width: 25rem;
  height: 13rem;
}
.flex-cyl-item {
  display: flex;
  flex-direction: column;
  flex-wrap: nowrap;
  justify-content: flex-end;
  margin-left: 1rem;
}
.flex-cyl-text1 {
  display: flex;
  flex-direction: row;
  justify-content: flex-end;
}
.flex-cyl-text {
  display: flex;
  flex-direction: row;
}
.cyl-text1 {
  display: flex;
  align-items: flex-end;
  font-size: 1.1rem;

  font-weight: 400;
  color: #ffffff;
}
.cyl-text2 {
  font-size: 1.1rem;

  font-weight: 400;
  color: #ffffff;
  display: flex;
  justify-content: center;
  align-items: center;
  width: 6.8rem;
  height: 1.5rem;
  background-image: url("../assets/智能设备.png");
  /* background-size: 100% 100%; */
  background-size: cover;
  background-repeat: no-repeat;
}
.cyl-h3 {
  font-size: 1rem;

  font-weight: 400;
  color: #ffffff;
  border: 0.1rem solid rgba(255, 255, 255, 0.805);
  border-radius: 0.2rem;
  position: absolute;
  padding: 0.1rem 0.5rem;
  top: 0.5rem;
  right: 0.5rem;
}
.cyl-number {
  font-size: 2.25rem;

  font-weight: 600;
  color: #ffffff;
  line-height: 1;
  padding: 0.4rem 0rem;
}
</style>